package cn.hd.authorService.controller.tableGenerate;

import cn.hd.authorService.service.tableGenerate.ITableGenerateService;
import cn.hd.commonService.annotations.LogAnnotation;
import cn.hd.commonService.entity.TableGenerate;
import cn.hd.commonService.enums.MethodType;
import cn.hd.commonService.enums.ServerEnum;
import cn.hd.commonService.vo.Query;
import cn.hd.commonService.vo.Result;
import cn.hd.commonService.vo.ServerEnumVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hd
 * @since 2022-10-07
 */
@RestController
@Api(tags = "代码生成器")
@RequestMapping("/tableGenerate")
public class TableGenerateController {


    @Autowired
    private ITableGenerateService tableGenerateService;
    @PostMapping("/save")
    @ApiOperation(value = "保存代码生成记录")
    @LogAnnotation(methodName = "保存代码生成记录",methodDesc = "保存代码生成记录",methodType = MethodType.FIND_TYPE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "functionName",value = "功能名称，例如:authorService",required = true),
            @ApiImplicitParam(name = "tableName",value = "待操作的表名,例如：table_generate",required = true)
    })
    public Result save(String recordId,String functionName,String tableName){
        Assert.hasText(functionName,"功能名称不能为空");
        Assert.hasText(tableName,"表名不能为空");
        tableGenerateService.saveTableGenerate(recordId,functionName,tableName);
        return Result.success("保存成功");
    }

    @GetMapping("/generate")
    @ApiOperation(value = "代码生成")
    @LogAnnotation(methodName = "代码生成",methodDesc = "代码生成",methodType = MethodType.FIND_TYPE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "recordId",value = "代码生成的主键",required = true)
    })
    public void generate(String recordId,HttpServletResponse response) throws IOException {
        Assert.hasText(recordId,"主键值不能为空");
        tableGenerateService.generate(recordId,response);
    }

    @PostMapping("/getListByPage")
    @ApiOperation(value = "获取代码生成记录")
    @LogAnnotation(methodName = "获取代码生成记录",methodDesc = "获取代码生成记录",methodType = MethodType.FIND_TYPE)
    public Result<TableGenerate> getListByPage(Query query) {
        return Result.success("获取成功",tableGenerateService.getListByPage(query));
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除代码生成记录")
    @LogAnnotation(methodName = "删除代码生成记录",methodDesc = "删除代码生成记录",methodType = MethodType.DELETE_TYPE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "recordIds",value = "代码生成记录的主键，多个用逗号分割")
    })
    public Result<TableGenerate> delete(String recordIds) {
        Assert.hasText(recordIds,"生成记录的主键不能为空");
        tableGenerateService.deleteTableGenerate(recordIds);
        return Result.success("操作成功");
    }

    @PostMapping("/getFunctionList")
    @ApiOperation(value = "获取当前系统中的功能列表")
    @LogAnnotation(methodName = "获取当前系统中的功能列表",methodDesc = "获取当前系统中的功能列表",methodType = MethodType.FIND_TYPE)
    public Result<ServerEnumVo> getFunctionList() {
        return Result.success("操作成功",ServerEnum.getList());
    }
}
